package com.leap.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.leap.entity.Media;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


@Mapper
public interface MediaMapper extends BaseMapper<Media> {

    @Select("SELECT media.id AS id, media.media_url AS mediaUrl, media.media_type AS mediaType, media.description AS description,\n" +
            "media.duration AS duration, media.width AS width, media.height AS height, media.create_time AS createTime FROM media,article_media,article\n" +
            "where media.id = article_media.media_id AND article.id = article_media.article_id AND article.id = #{id}")
    public List<Media> selectByArticleId(@Param("id") Long articleId);

    @Select("SELECT media_id FROM article_media where article_id = #{id} AND is_cover = 1")
    public Long getCoverById(Long id);

    @Delete("DELETE FROM article_media WHERE article_id = #{articleId}")
    public int deleteByArticleId(Long articleId);

    @Delete("DELETE FROM media WHERE media_url = #{url}")
    public int deleteByUrl(String url);


}
